package com.agv.web.async;

import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.agv.core.objectDomain.AdmCatalogo;
import com.agv.core.objectDomain.Catalogo;
import com.agv.core.objectDomain.ParamCatalogo;
import com.agv.core.services.ICatalogosService;
import com.agv.core.util.Constantes;

public class JSONReportAction extends DispatchAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private ICatalogosService catalogosService;

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse)
	 */

	public ActionForward obtenerDatosGrid(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		response.setContentType("text/html; charset=UTF-8");
		PrintWriter out = response.getWriter();

		JSONArray colNamesArray = new JSONArray();
		JSONArray colModelArray = new JSONArray();
		JSONObject responsedata = new JSONObject();

		AdmCatalogo admCatalogo = (AdmCatalogo) request.getSession()
				.getAttribute("catalogoObj");
		Catalogo catalogo = admCatalogo.getCatalogos().get(0);

		JSONObject object = new JSONObject();
		String sortType = "";

		object.element("name", catalogo.getNbCampoId());
		object.accumulate("index", catalogo.getNbCampoId());
		object.accumulate("key", "true");
		object.accumulate("sorttype", "int");
		colModelArray.add(object);
		colNamesArray.add(catalogo.getNbCampoId().replace("_", " "));

		for (ParamCatalogo paramCatalogo : catalogo.getParams()) {
			colNamesArray.add(paramCatalogo.getNbLeyenda());

			object = new JSONObject();
			sortType = "";
			if (paramCatalogo.getTpValidacion() != null) {
				if (paramCatalogo.getTpValidacion().equals(
						Constantes.VALIDACION_NUM)) {
					sortType = "int";
				} else if (paramCatalogo.getTpValidacion().equals(
						Constantes.VALIDACION_FECHA)) {
					sortType = "date";
				}
			}

			object.accumulate("name", paramCatalogo.getNbCampoTabla());
			object.accumulate("index", paramCatalogo.getNbCampoTabla());
			if (!sortType.equals("")) {
				object.accumulate("sorttype", sortType);
			}

			colModelArray.add(object);
		}

		responsedata.put("colNames", colNamesArray.toString());
		responsedata.put("colModel", colModelArray.toString());

		out.println(responsedata);
		return null;

	}

	public ActionForward buscarDatosCatalogo(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		response.setContentType("text/html; charset=UTF-8");
		PrintWriter out = response.getWriter();
		JSONObject responsedata = new JSONObject();
		JSONArray cellarray = new JSONArray();

		AdmCatalogo admCatalogo = (AdmCatalogo) request.getSession()
				.getAttribute("catalogoObj");

		String vlValor = (String) request.getSession().getAttribute(
				"paramBusquedaCat");

		Catalogo catalogo = admCatalogo.getCatalogos().get(0);

		List<Map<String, String>> mapa = catalogosService.buscar(vlValor,
				catalogo.getNbTabla(), admCatalogo.getNbCampoAutocompletar(),
				catalogo.getNbCampoId(), catalogo.getNbCampoEstatus(), catalogo
						.getParams());

		request.getSession().setAttribute("mapaObj", mapa);
		responsedata.put("total", mapa.size());
		responsedata.put("page", 1);
		responsedata.put("records", mapa.size());

		JSONArray cell = new JSONArray();
		JSONObject cellobj = new JSONObject();

		for (Map<String, String> map : mapa) {

			cell = new JSONArray();
			cellobj = new JSONObject();

			cellobj.put(catalogo.getNbCampoId(), map.get(catalogo
					.getNbCampoId()));
			cell.add(map.get(catalogo.getNbCampoId()));

			for (ParamCatalogo paramCatalogo : catalogo.getParams()) {
				cell.add(map.get(paramCatalogo.getNbCampoTabla()));
			}

			cellobj.put("cell", cell);
			cell.clear();
			cellarray.add(cellobj);

		}

		responsedata.put("rows", cellarray);
		out.println(responsedata);

		return null;
	}

	/**
	 * @param catalogosService
	 *            the catalogosService to set
	 */
	public void setCatalogosService(ICatalogosService catalogosService) {
		this.catalogosService = catalogosService;
	}

}
